package cn.how2go.api.util;

import java.sql.Time;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2019/4/11.
 */
public class DateUtil {

    public static Date getMaxDate(List<Date> list){

        Date result = null;
        if(list.size() ==0){
            return null;
        }
        if(list.size() == 1){
            return list.get(0);
        }

        if(list.size()>=2){
            result = list.get(0);
            for(int i=1;i<list.size();i++){
                Date temp = list.get(i);
                if(result.compareTo(temp)<0){
                    result = temp;
                }
            }
        }
        return result;
    }

    /**
     * 获取上一天的日期
     * @return
     */
    public static String getLastDate(){
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.DATE, -1);
        Date lastDate = ca.getTime(); //结果
        //求前一月ca.add(Calendar.MONTH, -1)，
        //前一天ca.add(Calendar.DATE, -1)
        //年份减1ca.add(Calendar.YEAR, -1)
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(lastDate);
    }

    public static Time strToTime(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Time(date.getTime());
    }


    /**
     * 获取上一天的日期
     * @return
     */
    public static String getLastDate(String patter){
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.DATE, -1);
        Date lastDate = ca.getTime(); //结果
        //求前一月ca.add(Calendar.MONTH, -1)，
        //前一天ca.add(Calendar.DATE, -1)
        //年份减1ca.add(Calendar.YEAR, -1)
        SimpleDateFormat sdf = new SimpleDateFormat(patter);
        return sdf.format(lastDate);
    }

    /**
     * 获取上N天的日期
     * @param  day 表示前N天
     * @return
     */
    public static String getLastDate(int day){
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.DATE, -1*day);
        Date lastDate = ca.getTime(); //结果
        //求前一月ca.add(Calendar.MONTH, -1)，
        //前一天ca.add(Calendar.DATE, -1)
        //年份减1ca.add(Calendar.YEAR, -1)
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(lastDate);
    }

    /**
     * 获取后N天的日期
     * @param  day 表示前N天
     * @return
     */
    public static String getDateLast(int day){
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.DATE, day);
        Date lastDate = ca.getTime(); //结果
        //求前一月ca.add(Calendar.MONTH, -1)，
        //前一天ca.add(Calendar.DATE, -1)
        //年份减1ca.add(Calendar.YEAR, -1)
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(lastDate);
    }

    /**
     * 获取上N天的日期
     * @param  day 表示前N天
     * @return
     */
    public static String getLastDate(int day, String patter){
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.DATE, -1*day);
        Date lastDate = ca.getTime(); //结果
        //求前一月ca.add(Calendar.MONTH, -1)，
        //前一天ca.add(Calendar.DATE, -1)
        //年份减1ca.add(Calendar.YEAR, -1)
        SimpleDateFormat sdf = new SimpleDateFormat(patter);
        return sdf.format(lastDate);
    }

    public static String getRandomDateTime(String patter){
        Random ran=new Random();
        int second = ran.nextInt(30);
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.SECOND, -1*second);
        Date lastDate = ca.getTime(); //结果
        SimpleDateFormat sdf = new SimpleDateFormat(patter);
        return sdf.format(lastDate);
    }

    public static String format(Date date,String patter){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(patter);
            return sdf.format(date);
        }catch (Exception e){
            return "-";
        }
    }

    public static String format(Date date){
        return format(date,"yyyy-MM-dd");
    }

    public static Date toDate(String date, String patter){
        SimpleDateFormat sdf = new SimpleDateFormat(patter);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }

    public static Date toDate(String date){
        return toDate(date,"yyyy-MM-dd");
    }

    public static Date toDateTime(String date){
        return toDate(date,"yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 两个时间差
     * @param endDate
     * @param nowDate
     * @return
     */
    public static String getDatePoor(Date endDate, Date nowDate) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        long sec = diff % nd % nh % nm / ns;
        if(day==0 &&  hour==0 && min==0) {
            return sec + "秒";
        }
        if(day==0 && hour==0){
            return min + "分钟" + sec + "秒";
        }
        if(day==0){
            return hour + "小时" + min + "分钟" + sec + "秒";
        }
        return day + "天" + hour + "小时" + min + "分钟" + sec + "秒";
    }

    public static String now2str(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }

    public static String formatDate(Date date,String patt){
        SimpleDateFormat sdf = new SimpleDateFormat(patt);
        return sdf.format(date);
    }

    public static Date str2date(String date,String patt){
        DateFormat sdf = new SimpleDateFormat(patt);

        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean timepk(String time1, int second,String patt){
        //跟当前时间比较
        //小于second
        boolean flag = true;
        DateFormat sdf = new SimpleDateFormat(patt);
        Date time = null;
        try {
            time = sdf.parse(time1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date now = new Date();
        long diff = now.getTime() - time.getTime();
        if(diff > second*1000){
            flag = false;
        }
        return flag;
    }

    /**
     * 当前时间是否超过 second
     * 超过返回true
     * 不超过返回fals
     * @param time1
     * @param second
     * @return
     */
    public static boolean timepk(Date time1, int second){
        boolean flag = true;
        Date now = new Date();
        long diff = now.getTime() - time1.getTime();
        if(diff < second*1000){
            flag = false;
        }
        return flag;
    }

    /**
     * 当前时间是否超过 second
     * @param time
     * @param now
     * @return
     */
    public static long timepk(Date time, Date now){
        long diff = now.getTime() - time.getTime();
        return diff/1000;
    }

    public static String today(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(new Date());
    }

    public static String hh(){
        SimpleDateFormat sdf = new SimpleDateFormat("HH");
        return sdf.format(new Date());
    }

    public static String hhmm(String time){
        SimpleDateFormat sdf = new SimpleDateFormat("HHmm");
        Date temp = null;
        String r = "0000";
        try {
            temp = str2date("2000-01-01 " + time + ":01", "yyyy-MM-dd HH:mm:ss");
            r = sdf.format(temp);
        }catch (Exception ex){
            System.out.println(ex.getMessage()+" ==> ERROR return 0000");
        }
        return r;
    }

    public static void main(String[] arg){
//        System.out.println(toDate("2019-04-30"));
//        System.out.println(getDatePoor(new Date(),toDate("2019-05-19 15:46:00","yyyy-MM-dd HH:mm:ss")));

        Time time = DateUtil.strToTime("00:01:01");
        System.out.println(time);

    }

}
